package com.dhcc.bpm.common.manager;

import com.dhcc.bpm.common.utils.Threads;

import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName AsyncManager
 * @Description 异步任务管理器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
public class AsyncManager
{
    /**
     * 操作延迟10毫秒
     */
    private final int OPERATE_DELAY_TIME = 10;

    /**
     * 异步操作任务调度线程池
     */
    private ScheduledExecutorService executor =Executors.newSingleThreadScheduledExecutor();

    /**
     * 单例模式
     */
    private static AsyncManager me = new AsyncManager();

    public static AsyncManager me()
    {
        return me;
    }

    /**
     * @Description 执行任务
     * @Author xiafayu
     * @Date 2020/2/24 19:56
     * @param task 任务
     * @return void
     * @throws
     **/
    public void execute(TimerTask task)
    {
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }

    /**
     * @Description 停止任务线程池
     * @Author xiafayu
     * @Date 2020/2/24 19:56
     * @param
     * @return void
     * @throws
     **/
    public void shutdown()
    {
        Threads.shutdownAndAwaitTermination(executor);
    }
}
